<?php

namespace app\api\controller;

use app\common\controller\Logined;
use app\common\service\WalletService;
use think\Db;

/*钱包处理
1.生成钱包地址
2.获取用户资产
3.刷新区块数据
4.获取币种涨跌幅
*/
class Wallet extends Logined {

    /*
    获取用户资金
    */
    public function getBalance(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }

        $time = time();
        $status = 0;
        $last = cache($uid.'balance');
        if(empty($last)){
            $status = 1;
            cache($uid.'balance',$time);  
        } else {
            if(($last+60)<$time){
                $status = 1;
                cache($uid.'balance',$time);
            }
        }
        $c_address = cache($uid.'c_address');
        if ($c_address) {
            $c_address = 0;
        } else {
            cache($uid.'c_address',1,10); 
            $c_address = 1;
        }
        

        $data  = [];
        $price = 0;
        $rmb   = 0;
        // $status = 1;
        //获取钱包列表
        $wallet_list = model('WalletModel')->getWalletList();
        foreach ($wallet_list as $key => $val) {
            $wallet_id = $val['id'];
            //获取钱包下面的币种列表
            $coin_list = Db::table('tp_coin')->alias('c')
                            ->join('tp_user_wallet_address a','a.cid=c.id and uid='.$uid,'LEFT')
                            ->where('c.status',1)
                            ->where('c.wid',$val['id'])
                            ->field('c.*,a.address')
                            ->select();
            $address_tag = [];
            if ($coin_list) {
                foreach ($coin_list as $k => $v) {
                    $address_info = [];
                    if (empty($v['address'])) {
                        //生成钱包地址
                        if ($c_address == 1) {
                            $address = WalletService::createAddress($uid,$v);
                            // var_dump($address);die();
                            if(!$address){
                                $this->retData(1, '币种'.$v['name'].'钱包地址获取失败');
                            }
                        } else {
                            $address = '';
                        }
                    } else {
                        $address = $v['address'];
                    }
                    // $address_info['address']   = $address;
                    // $address_info['cid']       = $v['id'];
                    // $address_info['coin_name'] = $v['name'];
                    // $address_tag[] = $address_info;
                    if($status == 1 && $val['is_recharge']==1){
                        //刷新区块数据
                        $res = WalletService::reQk($uid,$v);
                        if(!$res){
                            $this->retData(1, '币种'.$v['name'].'区块查询失败');
                        }
                        
                    }
                }
            }
            // $wallet_list[$key]['is_out']       = Db::table('tp_out_config')->where('wid',$val['id'])->find()?1:0;
            // $wallet_list[$key]['is_transfer']  = Db::table('tp_transfer_config')->where('wid',$val['id'])->find()?1:0;
            // $wallet_list[$key]['is_exchange']  = Db::table('tp_exchange_config')->where('wid',$val['id'])->find()?1:0;
            // $wallet_list[$key]['is_trade']     = Db::table('tp_trade_config')->where('wid',$val['id'])->find()?1:0;
            // $wallet_list[$key]['address'] = $address_tag;

            $wallet_list[$key]['amount'] =  model('UserBalanceModel')->getBalance($uid,$val['id']);
            $wallet_list[$key]['price']  = round($wallet_list[$key]['amount']*$val['price'],2);
            $price += $wallet_list[$key]['price'];
            // $wallet_list[$key]['rmb']    = round($wallet_list[$key]['amount']*$val['rmb'],2);
            // $rmb += $wallet_list[$key]['rmb'];           
        }
        $data['price'] = round($price,2);
        // $data['rmb']   = round($rmb,2);
        $data['balance_list']  = $wallet_list;
        $this->retData(0,'success',$data);
    }

    public function getWalletInfo(){
        $uid = $this->uid;
        $wid = P('wid');
        if (empty($uid) || empty($wid)) {
            $this->retData(1, '缺少参数');
        }
        $wallet  = model('WalletModel')->getSingleWallet($wid);
        $this->retData(0,'success',$data);
    }
    /*
    获取单个钱包余额
    */
    public function getSingleBalance(){
        $uid = $this->uid;
        $wid = P('wid')?:1;
        if (empty($uid) || empty($wid)) {
            $this->retData(1, '缺少参数');
        }
        $data = [];
        $wallet  = model('WalletModel')->getSingleWallet($wid);
        $balance = model('UserBalanceModel')->getBalance2($uid,$wid);

        // $data['wallet_name'] = $wallet['name'];
        // $data['amount'] = $balance;
        // $data['price']  = round($wallet['price'] * $balance,2);
        // $data['rmb']  = round($wallet['rmb'] * $data['price'],2);
        $this->retData(0,'success',$data);
    }

    /*
    钱包列表
     */
    public function getWalletList(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        //获取钱包列表
        $wallet_list = model('WalletModel')->getWalletList();
        foreach ($wallet_list as $key => $val) {
            $balance = model('UserBalanceModel')->getBalance($uid,$val['id']);
            $wallet_list[$key]['amount'] = $balance;
        }
        $this->retData(0,'success',$wallet_list);
    }

    /*
    获取地址列表
     */
    public function getAddress(){
        $uid = $this->uid;
        $wid = P('wid')?:1;
        if (empty($uid) || empty($wid)) {
            $this->retData(1, '缺少参数');
        }
        if (cache($uid.'getAddress')) {
            $this->retData(1, '重复提交');
        } else {
            cache($uid.'getAddress',1,10);
        }
        $data = model('WalletModel')->getSingleWallet($wid);
        if (empty($data)) {
            $this->retData(0,'success',[]);
        }
        $coin = Db::table('tp_coin')->alias('c')
                            ->join('tp_user_wallet_address a','a.cid=c.id and uid='.$uid,'LEFT')
                            ->where('c.status',1)
                            ->where('c.wid',$wid)
                            ->field('c.*,a.address')
                            ->select();
        if (empty($coin)) {
            $this->retData(0,'success',[]);
        } else {
            foreach ($coin as $key => $val) {
                if (empty($val['address'])) {
                    //生成钱包地址
                    $address = WalletService::createAddress($uid,$val);
                    // if(!$address){
                    //     $this->retData(1, '币种'.$val['name'].'钱包地址获取失败');
                    // }
                }
            }
        }
        // $data['address'] = $coin;
        $data = $coin;
        cache($uid.'getAddress',null);
        $this->retData(0,'success',$data);
    }


    /*
    获取币种总价值
     */
    public function getRmbOrPrice(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $balance = Db::table('tp_user_balance')->alias('b')
                        ->join('tp_coin_wallet w','w.id=b.wid')
                        ->where('b.uid',$uid)
                        ->field('b.balance balance,w.price,w.rmb')
                        ->select();
        $data  = [];
        $price = 0;
        $rmb   = 0;
        foreach ($balance as $key => $val) {
            $val['balance'] = $val['balance'] ?: 0;
            $single_price   = $val['balance'] * $val['price'];
            $single_rmb     = $val['balance'] * $val['rmb'];

            $price += $single_price;
            $rmb   += $single_rmb;
        }
        $this->retData(0,'success',['price'=>$price,'rmb'=>$rmb]);
    }
}